package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.mapper.OrderMapper;
import com.heima.pojo.domain.Order;
import com.heima.pojo.domain.PageResult;
import com.heima.pojo.dto.OrderPageQueryDto;
import com.heima.service.OrderService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper,Order> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public PageResult searchOrder(OrderPageQueryDto oDto) {
        Integer pageIndex = oDto.getPageIndex();
        Integer pageSize = oDto.getPageSize();
  /*      //总记录数
        Long totalCount = orderMapper.getTotalCount(oDto.getOrderNo(),oDto.getStartDate(),oDto.getEndDate());
        //总页数
        Long totalPage = (totalCount + pageIndex - 1)/pageSize;*/
        Page<Order> page = new Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<Order> le = new LambdaQueryWrapper<Order>().eq(oDto.getOrderNo() != null, Order::getOrderNo, oDto.getOrderNo())
                .ge(oDto.getStartDate() != null, Order::getCreateTime, oDto.getStartDate())
                .le(oDto.getEndDate() != null, Order::getCreateTime, oDto.getEndDate());
        Page<Order> list = orderMapper.selectPage(page, le);

        return new PageResult<>(pageIndex,pageSize,list.getPages(),list.getTotal(),list.getRecords());
    }

    @Override
    public Integer searchOrderCount(LocalDateTime start, LocalDateTime end) {
        Integer count = orderMapper.selectOrderCount(start,end);
        return count;
    }

    @Override
    public Integer searchOrderAmount(LocalDateTime start, LocalDateTime end) {
        Integer count = orderMapper.selectOrderAmount(start,end);
        return count;
    }
}
